/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace Xceed.Silverlight.ListBox
{
  public abstract class IndicatorBase : Control
  {
    #region Constructor
    internal IndicatorBase()
    {
    }
    #endregion

    #region Minimum Property

    public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(
      "Minimum",
      typeof( double ),
      typeof( IndicatorBase ),
      new PropertyMetadata( 0d ) );

    public double Minimum
    {
      get
      {
        return ( double )this.GetValue( IndicatorBase.MinimumProperty );
      }
      set
      {
        this.SetValue( IndicatorBase.MinimumProperty, value );
      }
    }

    #endregion

    #region Maximum Property

    public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
      "Maximum",
      typeof( double ),
      typeof( IndicatorBase ),
      new PropertyMetadata( 1d ) );

    public double Maximum
    {
      get
      {
        return ( double )this.GetValue( IndicatorBase.MaximumProperty );
      }
      set
      {
        this.SetValue( IndicatorBase.MaximumProperty, value );
      }
    }

    #endregion

    #region AnimatedValue Property

    public static readonly DependencyProperty AnimatedValueProperty = DependencyProperty.Register(
      "AnimatedValue",
      typeof( object ),
      typeof( IndicatorBase ),
      new PropertyMetadata( null ) );

    public object AnimatedValue
    {
      get
      {
        return ( object )this.GetValue( IndicatorBase.AnimatedValueProperty );
      }
      set
      {
        this.SetValue( IndicatorBase.AnimatedValueProperty, value );
      }
    }

    #endregion

    #region AnimationDuration Property

    public static readonly DependencyProperty AnimationDurationProperty = DependencyProperty.Register(
      "AnimationDuration",
      typeof( int ),
      typeof( IndicatorBase ),
      new PropertyMetadata( 1000 ) );

    public int AnimationDuration
    {
      get
      {
        return ( int )this.GetValue( IndicatorBase.AnimationDurationProperty );
      }
      set
      {
        this.SetValue( IndicatorBase.AnimationDurationProperty, value );
      }
    }

    #endregion

    #region RefreshRate Property

    public static readonly DependencyProperty RefreshRateProperty = DependencyProperty.Register(
      "RefreshRate",
      typeof( int ),
      typeof( IndicatorBase ),
      new PropertyMetadata( 30,
        new PropertyChangedCallback( IndicatorBase.OnRefreshRateChanged ) ) );

    public int RefreshRate
    {
      get
      {
        return ( int )this.GetValue( IndicatorBase.RefreshRateProperty );
      }
      set
      {
        this.SetValue( IndicatorBase.RefreshRateProperty, value );
      }
    }

    private static void OnRefreshRateChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
      IndicatorBase indicator = sender as IndicatorBase;

      if( indicator == null )
        return;

      indicator.OnRefreshRateChanged( ( int )e.OldValue, ( int )e.NewValue );
    }

    #endregion

    #region FadeOutAnimationDuration Property

    public static readonly DependencyProperty FadeOutAnimationDurationProperty = DependencyProperty.Register(
      "FadeOutAnimationDuration",
      typeof( int ),
      typeof( IndicatorBase ),
      new PropertyMetadata( 0,
        new PropertyChangedCallback( IndicatorBase.OnFadeOutAnimationDurationChanged ) ) );

    public int FadeOutAnimationDuration
    {
      get
      {
        return ( int )this.GetValue( IndicatorBase.FadeOutAnimationDurationProperty );
      }
      set
      {
        this.SetValue( IndicatorBase.FadeOutAnimationDurationProperty, value );
      }
    }

    private static void OnFadeOutAnimationDurationChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e )
    {
      IndicatorBase indicator = sender as IndicatorBase;

      if( indicator == null )
        return;

      indicator.OnFadeOutAnimationDurationChanged( ( int )e.OldValue, ( int )e.NewValue );
    }

    #endregion

    internal void OnFadeOutAnimationDurationChanged( int oldValue, int newValue )
    {
      if( m_killTimer != null )
        m_killTimer.Interval = TimeSpan.FromMilliseconds( newValue );
    }

    internal void OnRefreshRateChanged( int oldValue, int newValue )
    {
      if( ( m_timer != null ) && ( newValue > 0 ) )
      {
        m_timer.Interval = TimeSpan.FromMilliseconds( 1000 / newValue );
      }
    }

    internal void StartTimer()
    {
      if( m_timer == null )
      {
        m_timer = new DispatcherTimer();
        m_timer.Tick += this.OnTimerTick;

        int refreshRate = this.RefreshRate;

        if( refreshRate > 0 )
          m_timer.Interval = TimeSpan.FromMilliseconds( 1000 / refreshRate );
      }

      if( !m_timer.IsEnabled )
        m_timer.Start();
    }

    internal void StopTimer()
    {
      if( m_timer == null )
        return;

      m_timer.Stop();
    }

    internal void StartKillTimer()
    {
      if( m_killTimer == null )
      {
        m_killTimer = new DispatcherTimer();
        m_killTimer.Tick += this.OnKillTimerTick;

        int refreshRate = this.RefreshRate;

        if( refreshRate > 0 )
          m_killTimer.Interval = TimeSpan.FromMilliseconds( 1000 / refreshRate );
      }

      if( !m_killTimer.IsEnabled )
        m_killTimer.Start();
    }

    internal void StopKillTimer()
    {
      if( m_killTimer == null )
        return;

      m_killTimer.Stop();
    }

    internal void OnTimerTick( object sender, EventArgs e )
    {
      int oldTickCount = m_tickCounts;
      m_tickCounts = Environment.TickCount;
      double minimum = this.Minimum;

      m_ratio = ( m_ratio + ( ( double )( m_tickCounts - oldTickCount ) / ( double )this.AnimationDuration ) ) % 1;

      this.AnimatedValue = minimum + ( m_ratio * ( this.Maximum - minimum ) );
    }

    internal virtual void OnKillTimerTick( object sender, EventArgs e )
    {
      this.StopTimer();
      this.StopKillTimer();
    }

    #region Private fields

    private DispatcherTimer m_timer = null;
    private DispatcherTimer m_killTimer = null;
    private double m_ratio = 0;
    private int m_tickCounts = Environment.TickCount;

    #endregion
  }
}
